#include "clickedlabel.h"
#include <QMouseEvent>
#include <QLabel>
#include <QDebug>

ClickedLabel::ClickedLabel(QWidget *parent) : QLabel(parent), m_curstate(ClickLbState::Normal)
{
    this->setCursor(Qt::PointingHandCursor);
}

bool ClickedLabel::setState(ClickLbState state)
{
    m_curstate = state;
    if (m_curstate == ClickLbState::Normal) {
        setProperty("state", m_normal);
        repolish(this);
    }
    else if (m_curstate == ClickLbState::Selected) {
        setProperty("state", m_selected);
        repolish(this);
    }

    return true;
}

ClickLbState ClickedLabel::getCurState() const
{
    return m_curstate;
}

bool ClickedLabel::setCurState(ClickLbState state)
{
    m_curstate = state;
    if (m_curstate == ClickLbState::Normal) {
        setProperty("state", m_normal);
        repolish(this);
    } else if (m_curstate == ClickLbState::Selected) {
        setProperty("state", m_selected);
        repolish(this);
    }

    return true;
}

void ClickedLabel::setState(QString normal, QString hover, QString press, QString select, QString select_hover, QString select_press)
{
    m_normal = normal;
    m_normal_hover = hover;
    m_normal_press = press;

    m_selected = select;
    m_selected_hover = select_hover;
    m_selected_press = select_press;

    setProperty("state",normal);
    repolish(this);
}

void ClickedLabel::resetNormalState()
{
    m_curstate = ClickLbState::Normal;
    setProperty("state", m_normal);
    repolish(this);
}

void ClickedLabel::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        if (m_curstate == ClickLbState::Normal) {
            m_curstate = ClickLbState::Selected;
            setProperty("state", m_selected_press);
            repolish(this);
            update();
        } else{
            m_curstate = ClickLbState::Normal;
            setProperty("state", m_normal_press);
            repolish(this);
            update();
        }

        return ;
    }
    // 调用基类的mousePressEvent以保证正常的事件处理
    QLabel::mousePressEvent(e);
}

void ClickedLabel::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        if (m_curstate == ClickLbState::Normal) {
            setProperty("state", m_normal_hover);
            repolish(this);
            update();
        } else {
            setProperty("state", m_selected_hover);
            repolish(this);
            update();
        }

        emit clicked(this->text(), m_curstate);

        return;
    }
    // 调用基类的mousePressEvent以保证正常的事件处理
    QLabel::mousePressEvent(e);
}

void ClickedLabel::enterEvent(QEvent *e)
{
    // 在这里处理鼠标悬停进入的逻辑
    if (m_curstate == ClickLbState::Normal) {
        // qDebug() << "enter, change to normal hover: " << m_normal_hover;
        setProperty("state", m_normal_hover);
        repolish(this);
        update();
    } else {
        // qDebug() << "enter , change to selected hover: " << m_selected_hover;
        setProperty("state", m_selected_hover);
        repolish(this);
        update();
    }

    QLabel::enterEvent(e);
}

void ClickedLabel::leaveEvent(QEvent *e)
{
    // 在这里处理鼠标悬停离开的逻辑
    if (m_curstate == ClickLbState::Normal){
        // qDebug() << "leave, change to normal : " << m_normal;
        setProperty("state", m_normal);
        repolish(this);
        update();
    } else{
        // qDebug() << "leave , change to normal hover: " << m_selected;
        setProperty("state", m_selected);
        repolish(this);
        update();
    }
    QLabel::leaveEvent(e);
}
